<?php
 /*
  *
  * 图片处理静态类
  * 包括 代码生成图片,缩略图,加水印功能,把图片变友灰色四个功能
  *
  */

  class image {

	static function info($img){
		$imageinfo = getimagesize($img);
		if($imageinfo===false)return false;

		$imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1));
		$imagesize = filesize($img);

		$info = array(
				'width'=>$imageinfo[0],
				'height'=>$imageinfo[1],
				'type'=>$imagetype,
				'typelable'=>$imageinfo[2],
				'size'=>$imagesize,
				'mime'=>$imageinfo['mime']
				);
		return $info;
	}


	/*
	 * 生成一张JPG图
	 * @param $w 生成的图片的宽度
	 * @param $h 生成的图片的高度
	 * @param $bg 图片背景
	 * @param $str 在图片上写的文字
	 * @param $strSize $strColor $strXY文字的大小,颜色,X,Y坐标
	 * @param $istruecolor 是否真彩图片
	 */
	static function createImg($w,$h,$bg,$str,$strSize,$strColor,$strXY,$istruecolor){
		header("Content-type:image/png");
		//生成画面
		if($istruecolor){
			$im = imagecreatetruecolor($w,$h);
		}else{
			$im = imagecreate($w,$h);
		}
		//设置颜色
		$bgcolor = imagecolorallocate($im,$bg[0],$bg[1],$bg[2]);
		$strcolor = imagecolorallocate($im,$strColor[0],$strColor[1],$strColor[2]);
		//平铺背景
		imagefill($im,0,0,$bgcolor);
		//写入文字
		imagestring($im,$strSize,$strXY[0],$strXY[1],$str,$strcolor);
		imagejpeg($im);
		imagedestroy($im);
	}


	/*
	 * 生成缩略图的缩放比率
	 * @param $srcwidth $srcheight 原图高宽
	 * @param $dstw,$dsth 缩放时最大高宽
	 * @return array
	 */
	static function getpercent($srcwidth,$srcheight,$dstw,$dsth){
		if (empty($srcwidth) || empty($srcheight) || ($srcwidth <= $dstw && $srcheight <= $dsth)) $w = $srcwidth ;$h = $srcheight;
    	if ((empty($dstw) || $dstw == 0)  && $dsth > 0 && $srcheight > $dsth) {
			$h = $dsth;
			$w = round($dsth / $srcheight * $srcwidth);
		} elseif ((empty($dsth) || $dsth == 0) && $dstw > 0 && $srcwidth > $dstw) {
			$w = $dstw;
			$h = round($dstw / $srcwidth * $srcheight);
		} elseif ($dstw > 0 && $dsth > 0) {
			if (($srcwidth / $dstw) < ($srcheight / $dsth)) {
					$w = round($dsth / $srcheight * $srcwidth);
					$h = $dsth;
			} elseif (($srcwidth / $dstw) > ($srcheight / $dsth)) {
					$w = $dstw;
					$h = round($dstw / $srcwidth * $srcheight );
			} else {
				$h = $dstw;
				$w = $dsth;
			}
		}
		$array['w']  = $w;
		$array['h']  = $h;
		return $array;

	}

	/*
	 * 缩放图片
	 * @param $image 需要缩放的图片
	 * @param $filename 缩放后的图片名称
	 * @param $maxwidth $maxheight 缩放的最大高宽
	 * @param $suffix 如果没有设置第二个参数,参动态生成图片时加的后缀
	 * @param $autocut 是否自动切割
	 * @param $ftp false
	 * @return new file name
	 */
	static function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 200, $suffix='', $autocut = 0, $ftp = 0){
		//检查缩放图片环境
		if(!self::check($image)) return false;
		//获取图片信息(大小和类型)
		$info  = image::info($image);
		if($info === false) return false;

		$srcwidth  = $info['width'];
		$srcheight = $info['height'];
		$pathinfo  = pathinfo($image);
		$type =  $pathinfo['extension'];

		if(!$type) $type = $info['type'];
		$type = strtolower($type);
		unset($info);

		//获取缩放比率,根据比率生成缩放后的高度和宽度
		$creat_arr = self::getpercent($srcwidth,$srcheight,$maxwidth,$maxheight);
		$createwidth = $width = $creat_arr['w'];
		$createheight = $height = $creat_arr['h'];

		$psrc_x = $psrc_y = 0;

		//如果自动剪切,设置剪切的高宽度
		if($autocut && $maxwidth > 0 && $maxheight > 0) {
			if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) {
				$width = $maxheight/$height*$width;
				$height = $maxheight;
			}elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width) {
				$height = $maxwidth/$width*$height;
				$width = $maxwidth;
			}
			$createwidth = $maxwidth;
			$createheight = $maxheight;
		}

		//根据源图片类型,建立相同类型的及新高宽的图像
		$createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type);
		$srcimg = $createfun($image);
		if($type != 'gif' && function_exists('imagecreatetruecolor'))
			$thumbimg = imagecreatetruecolor($createwidth, $createheight);
		else
			$thumbimg = imagecreate($width, $height);

		//进行缩放
		if(function_exists('imagecopyresampled'))
			imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
		else
			imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height,  $srcwidth, $srcheight);

		//如果是GID或PNG,则设置个透明背景
		if($type=='gif' || $type=='png') {
			$background_color  =  imagecolorallocate($thumbimg,  0, 255, 0);  //  指派一个绿色
			imagecolortransparent($thumbimg, $background_color);  //  设置为透明色，若注释掉该行则输出绿色的图
		}
		//--
		if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg,0);
		//生成缩放后图像
		$imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
		if(empty($filename)) $filename  = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type;
		$imagefun($thumbimg, $filename);
		imagedestroy($thumbimg);
		imagedestroy($srcimg);
		if($ftp) {
			@unlink($image);
		}
		return $filename;

	}

	/*
	 * 给图片加水印
	 * @param $source 要加水印的图片
	 * @param $target 加水印后保存成图片
	 * @param $w_pos  水印位置 1-10
	 * @param $w_img  加水印图片,可为空,优先于文字
	 * @param $w_text $w_font $w_color 加水印文字,及其字体,颜色
	 * @return true
	 */

	static function watermark($source, $target = '', $w_pos = '', $w_img = '', $w_text = '',$w_font = 8, $w_color = '#ff0000'){
		//初始化默认值
		$w_pos = $w_pos ? $w_pos : 1;

		if(!self::check($source)) return false;

		if(!$target) $target = $source;

		//$source_info = getimagesize($source);
		$source_info = image::info($source);
		$source_w    = $source_info['width'];
		$source_h    = $source_info['height'];

		switch($source_info['typelable']) {
			case 1 :
				$source_img = imagecreatefromgif($source);
				break;
			case 2 :
				$source_img = imagecreatefromjpeg($source);
				break;
			case 3 :
				$source_img = imagecreatefrompng($source);
				break;
			default :
				return false;
		}

		if(!empty($w_img) && file_exists($w_img)) {
			$ifwaterimage = 1;
			$water_info   = image::info($w_img);
			$width        = $water_info['width'];
			$height       = $water_info['height'];

			if($source_w < $width || $source_h < $$height) return false;

			switch($water_info['typelable']) {
				case 1 :
					$water_img = imagecreatefromgif($w_img);
					break;
				case 2 :
					$water_img = imagecreatefromjpeg($w_img);
					break;
				case 3 :
					$water_img = imagecreatefrompng($w_img);
					break;
				default :
					return;
			}
		} else {
			$ifwaterimage = 0;
			$temp = imagettfbbox(ceil($w_font*2.5), 0, PC_PATH.'libs/data/font/elephant.ttf', $w_text);
			$width = $temp[2] - $temp[6];
			$height = $temp[3] - $temp[7];
			unset($temp);
		}
		switch($w_pos) {
			case 1:
				//左上
				$wx = 5;
				$wy = 5;
				break;
			case 2:
				//中上
				$wx = ($source_w - $width) / 2;
				$wy = 0;
				break;
			case 3:
				//右上
				$wx = $source_w - $width;
				$wy = 0;
				break;
			case 4:
				//左中
				$wx = 0;
				$wy = ($source_h - $height) / 2;
				break;
			case 5:
				//中中
				$wx = ($source_w - $width) / 2;
				$wy = ($source_h - $height) / 2;
				break;
			case 6:
				//右中
				$wx = $source_w - $width;
				$wy = ($source_h - $height) / 2;
				break;
			case 7:
				//左下
				$wx = 0;
				$wy = $source_h - $height;
				break;
			case 8:
				//中下
				$wx = ($source_w - $width) / 2;
				$wy = $source_h - $height;
				break;
			case 9:
				//右下
				$wx = $source_w - $width;
				$wy = $source_h - $height;
				break;
			case 10:
				//随机
				$wx = rand(0,($source_w - $width));
				$wy = rand(0,($source_h - $height));
				break;
			default:
				$wx = rand(0,($source_w - $width));
				$wy = rand(0,($source_h - $height));
				break;
		}

		if($ifwaterimage) {
			if($water_info['typelable'] == 3) {
				imagecopy($source_img, $water_img, $wx, $wy, 0, 0, $width, $height);
			} else {
				imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, 100);
			}
		} else {
			if(!empty($w_color) && (strlen($w_color)==7)) {
				$r = hexdec(substr($w_color,1,2));
				$g = hexdec(substr($w_color,3,2));
				$b = hexdec(substr($w_color,5));
			} else {
				return;
			}
			imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
		}

		switch($source_info['typelable']) {
			case 1 :
				imagegif($source_img, $target);
				break;
			case 2 :
				imagejpeg($source_img, $target, 100);
				break;
			case 3 :
				imagepng($source_img, $target);
				break;
			default :
				return;
		}

		if(isset($water_info)) {
			unset($water_info);
		}
		if(isset($water_img)) {
			imagedestroy($water_img);
		}
		unset($source_info);
		imagedestroy($source_img);
		return true;

	}





	/*
	 * 变成灰色图片
	 */
	 static function grayImg($resimg,$grayimg){
		//检查图片环境
		if(!self::check($resimg)) return false;

		$source_info   = image::info($resimg);
		$img_width     = $source_info['width'];
		$img_height    = $source_info['height'];

		switch($source_info['typelable']) {
			case 1 :
				$image = imagecreatefromgif($resimg);
				break;
			case 2 :
				$image = imagecreatefromjpeg($resimg);
				break;
			case 3 :
				$image = imagecreatefrompng($resimg);
				break;
			default :
				return false;
		}


		for ($y = 0; $y <$img_height; $y++) {
		   for ($x = 0; $x <$img_width; $x++) {
			$gray = (ImageColorAt($image, $x, $y) >> 8) & 0xFF;
			imagesetpixel ($image, $x, $y, ImageColorAllocate ($image, $gray,$gray,$gray));
		   }
		}

		switch($source_info['typelable']) {
			case 1 :
				imagegif($image, $grayimg);
				break;
			case 2 :
				imagejpeg($image, $grayimg, 100);
				break;
			case 3 :
				imagepng($image, $grayimg);
				break;
			default :
				return;
		}

		imagedestroy($image);

	 }





	/*
	 * 检测环境
	 *
	 */
	static function check($image) {
		return extension_loaded('gd') && preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && file_exists($image) && function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
	}



  }









?>